package com.bluesky.android.activities;

import com.bluesky.android.newlove.R;
import java.io.File;
import java.util.HashMap;
import java.util.List;

import net.robotmedia.acv.utils.FileUtils;

import com.bluesky.android.utils.*;
import com.bluesky.android.webservice.MangaChapter;
import com.bluesky.android.webservice.MangaStory;
import com.bluesky.android.webservice.MangaChapter.DownloadedEnum;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;

public class MangaChapterAdapter extends BaseAdapter  implements Filterable {
	
	private LayoutInflater mInflater;	
	private Context context;
	private final Activity activity;
    private final List mangaList;  
	
	public MangaChapterAdapter(Activity activity, List objects, Context context) {
        //super(activity, R.layout.chapter_list_item , objects);
        this.activity = activity;
        this.mangaList = objects;
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }
	
	public View getView(final int position, View convertView, ViewGroup parent) {
	        View rowView = convertView;
	        MangaChapterItemView sqView = null;
	 
	        if(rowView == null)
	        {
	            // Get a new instance of the row layout view
	            //LayoutInflater inflater = activity.getLayoutInflater();
	            rowView = mInflater.inflate(R.layout.chapter_list_item, null);
	 
	            // Hold the view objects in an object,
	            // so they don't need to be re-fetched
	            sqView = new MangaChapterItemView();
	            sqView.title = (TextView) rowView.findViewById(R.id.title);
	            sqView.date = (TextView) rowView.findViewById(R.id.date);
	            sqView.download = (Button) rowView.findViewById(R.id.download);
	            sqView.readSave = (Button) rowView.findViewById(R.id.readSave);
	            sqView.downloadMessage = (TextView) rowView.findViewById(R.id.downloadMessage);
	           
	            // Cache the view objects in the tag,
	            // so they can be re-accessed later
	            rowView.setOnClickListener(new View.OnClickListener() {
	            	private int pos = position;
	            	 
	            	public void onClick(View v) {
	            		//Toast.makeText(activity, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();   
	            	}
	            });
	            	 
	            sqView.download.setOnClickListener(new View.OnClickListener() {   	
	            	 	            	
	            	public void onClick(View v) {
	            		//int posi = parent.getChildAt(pos);
	            		int buttonID = v.getId();	            		
	            		MangaChapter mangaChapter =(MangaChapter) mangaList.get(buttonID);
	            		
	            		//download data
	            		if(mangaChapter.getChapterIsDownloading() == DownloadedEnum.NotDownload){
		            		mangaChapter.setChapterIsDownloading(DownloadedEnum.Downloading);
		            		//add to download manager for downloading data
		            		DownloadChapterDataManager.getInstance(context, MangaChapterAdapter.this).AddChapter(mangaChapter);		            		
		            		notifyDataSetChanged();
	            		}else{
	            			//delete content
	            			File dir = new File(Environment.getExternalStorageDirectory(), "MangaReader");
	            			dir = new File(dir, String.valueOf(mangaChapter.getMangaId()));
	            			if(dir.exists()){
	            				dir = new File(dir, String.valueOf(mangaChapter.getChapterId()));
		            			if(dir.exists()){
		            				FileUtils.deleteDirectory(dir);
		            			}
	            			}
	            			mangaChapter.setSaveDataOnDisk((short) 0);
	            			MangaDB mangaDB = new MangaDB(context);
	            			mangaDB.open();
	            			mangaDB.updateManagaChapter(mangaChapter);
	            			mangaDB.close();
	            			notifyDataSetChanged();	            			
	            		}
	            		
	            	 
	            	}
	            });
	            sqView.readSave.setOnClickListener(new View.OnClickListener() {
	            	public void onClick(View v) {
	            		//int posi = sqView.readOnline.getId();
	            		int buttonID = v.getId();
	            		MangaChapter mangaChapter =(MangaChapter) mangaList.get(buttonID);
	            		mangaChapter.setSaveDataOnDisk((short)1);
	            		
	            		Intent i = new Intent(context, ComicViewerActivity.class);
	            		try{
	            			i.putExtra(ComicViewerActivity.CHAPTER_DATA_URL, mangaChapter.getLink());
		            		i.putExtra(ComicViewerActivity.READ_ONLINE, false);
		            		i.putExtra(ComicViewerActivity.CHAPTER_ID, mangaChapter.getChapterId());
		            		context.startActivity(i);
	            		}catch(Exception ex){
	            			ex.printStackTrace();
	            		}
	            	}
	            });
	            rowView.setTag(sqView);
	        } else {
	            sqView = (MangaChapterItemView) rowView.getTag();
	        }
	 
	       
	        MangaChapter currentChapter =(MangaChapter) mangaList.get(position);
	        if(currentChapter.getChapterIsDownloading() == DownloadedEnum.Downloading){
	        	sqView.download.setEnabled(false);
	        	sqView.readSave.setEnabled(false);
	        	sqView.downloadMessage.setVisibility(View.VISIBLE);
	        }else{
	        	if(currentChapter.getChapterIsDownloading() == DownloadedEnum.NotDownload){
	        		sqView.downloadMessage.setVisibility(View.GONE);
		        	sqView.download.setEnabled(true);
		        	sqView.readSave.setEnabled(true);
		        	sqView.download.setText("Download");
		        	sqView.readSave.setText("Read&Save");
	        	}else{
	        		sqView.downloadMessage.setVisibility(View.GONE);
		        	sqView.download.setEnabled(true);
		        	sqView.readSave.setEnabled(true);
		        	sqView.download.setText("Delete");
		        	sqView.readSave.setText("Read");
	        	}        		
	        	
	        }         
            	
            sqView.download.setId(position);
            sqView.readSave.setId(position);
	        sqView.title.setText(MangaUtility.unescape(currentChapter.getTitle() ) );
	        sqView.date.setText("Date: " + currentChapter.getDate() );        
	        return rowView;
	    }
	 
	    static class MangaChapterItemView {
	    	Button download;
	    	Button readSave;
	    	TextView title;
	        TextView date;
	        TextView downloadMessage;
	    }

		public Filter getFilter() {
			// TODO Auto-generated method stub
			return null;
		}
		public int getCount() {
			// TODO Auto-generated method stub
			return mangaList.size();
		}
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mangaList.get(arg0);
		}
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
}
